package org.jeecg.modules.exPingbiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.exPingbiao.entity.ExPingbiaoExpert;
import org.jeecg.modules.exPingbiao.entity.ExPingbiaoExpertDetail;
import org.jeecg.modules.exPingbiao.entity.ExPingbiaoSectionExpertConfig;
import org.jeecg.modules.exPingbiao.mapper.ExPingbiaoExpertDetailMapper;
import org.jeecg.modules.exPingbiao.mapper.ExPingbiaoExpertMapper;
import org.jeecg.modules.exPingbiao.mapper.ExPingbiaoSectionExpertConfigMapper;
import org.jeecg.modules.exPingbiao.service.IExPingbiaoExpertDetailService;
import org.jeecg.modules.exPingbiao.service.IExPingbiaoExpertRecordService;
import org.jeecg.modules.exPingbiao.service.IExPingbiaoExpertService;
import org.jeecg.modules.exPingbiao.vo.ExPingbiaoConformanceReview;
import org.jeecg.modules.exPingbiao.vo.ExZbwjConformanceReviewDetaiScoreSumVO;
import org.jeecg.modules.exPingbiao.vo.ExZbwjConformanceReviewDetaiScoreVO;
import org.jeecg.modules.exPingbiao.vo.ExZbwjConformanceReviewDetailVO;
import org.jeecg.modules.exTender.entity.ExTenderList;
import org.jeecg.modules.exTender.service.IExTenderListService;
import org.jeecg.modules.exTenderFile.entity.ExZbwjConformanceReview;
import org.jeecg.modules.exTenderFile.entity.ExZbwjConformanceReviewDetail;
import org.jeecg.modules.exTenderFile.entity.ExZbwjScoreStandardDetail;
import org.jeecg.modules.exTenderFile.entity.ScoreChapter;
import org.jeecg.modules.exTenderFile.mapper.ExZbwjScoreStandardDetailMapper;
import org.jeecg.modules.exTenderFile.service.IExZbwjConformanceReviewDetailService;
import org.jeecg.modules.exTenderFile.service.IExZbwjConformanceReviewService;
import org.jeecg.modules.exTenderFile.service.IScoreChapterService;
import org.jeecg.modules.exTenderFile.vo.ScoreChapterTree;
import org.jeecg.modules.exTenderFile.vo.TenderScoreVO;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ExPingbiaoExpertRecordServiceImpl implements IExPingbiaoExpertRecordService {
    @Autowired
    private IExZbwjConformanceReviewService exZbwjConformanceReviewService;
    @Autowired
    private IExZbwjConformanceReviewDetailService exZbwjConformanceReviewDetailService;
    @Autowired
    private IExTenderListService exTenderListService;
    @Autowired
    private IExPingbiaoExpertService exPingbiaoExpertService;
    @Autowired
    private IExPingbiaoExpertDetailService exPingbiaoExpertDetailService;
    @Autowired
    private ExPingbiaoExpertDetailMapper exPingbiaoExpertDetailMapper;
    @Autowired
    private ExPingbiaoExpertMapper exPingbiaoExpertMapper;
    @Autowired
    private IExTenderListService tenderListService;
    @Autowired
    private ExZbwjScoreStandardDetailMapper scoreStandardDetailMapper;
    @Autowired
    private IScoreChapterService scoreChapterService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ExPingbiaoSectionExpertConfigMapper sectionExpertConfigMapper;

    @Override
    public ExPingbiaoConformanceReview getConformanceReviewRecord(String conformanceReviewId, String expertId) {
        ExPingbiaoConformanceReview conformanceReview = new ExPingbiaoConformanceReview();
        List<ExZbwjConformanceReviewDetailVO> conformanceReviewDetailVOList = new ArrayList<>();
        ExZbwjConformanceReview review = exZbwjConformanceReviewService.getById(conformanceReviewId);
        String bidSectionId = review.getBidSectionIds();

        List<ExZbwjConformanceReviewDetail> exZbwjConformanceReviewDetails = exZbwjConformanceReviewDetailService.selectByMainId(conformanceReviewId);
        for (int i = 0; i < exZbwjConformanceReviewDetails.size(); i++) {
            ExZbwjConformanceReviewDetailVO vo = new ExZbwjConformanceReviewDetailVO();
            ExZbwjConformanceReviewDetail exZbwjConformanceReviewDetail = exZbwjConformanceReviewDetails.get(i);
            BeanUtils.copyProperties(exZbwjConformanceReviewDetail, vo);
            conformanceReviewDetailVOList.add(vo);
            if (StringUtils.isNotEmpty(expertId)) {
                // 赋值审查项下 指定专家的 所有投标人的  审查项 评分记录
                vo.setScoreList(getAllTenderScore(exZbwjConformanceReviewDetail.getConformanceReviewId(), expertId));
            } else {
                vo.setSumList(getConformanceReviewSum(exZbwjConformanceReviewDetail.getConformanceReviewId(), exZbwjConformanceReviewDetail.getId()));
            }
        }
        conformanceReview.setConformanceReviewDetailList(conformanceReviewDetailVOList);

        //投标人
        setExTenderList(conformanceReview, bidSectionId);
        return conformanceReview;
    }

    private void setExTenderList(ExPingbiaoConformanceReview conformanceReview, String bidSectionId) {
        LambdaQueryWrapper<ExTenderList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExTenderList::getBidSectionId, bidSectionId);
        List<ExTenderList> tenderLists = exTenderListService.list(queryWrapper);
        conformanceReview.setExTenderLists(tenderLists);
    }

    // 标段id, 专家id， 审查项id
    private List<ExZbwjConformanceReviewDetaiScoreVO> getAllTenderScore(String conformanceReviewId, String expertId) {
        LambdaQueryWrapper<ExPingbiaoExpert> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExPingbiaoExpert::getRealBusinessId, conformanceReviewId);
        queryWrapper.eq(ExPingbiaoExpert::getExpertId, expertId);
        List<ExPingbiaoExpert> pingbiaoExpertList = exPingbiaoExpertService.list(queryWrapper);
        if (CollectionUtils.isEmpty(pingbiaoExpertList)) {
            return new ArrayList<>();
        }
        List<ExZbwjConformanceReviewDetaiScoreVO> detaiScoreVOS = new ArrayList<>();
        pingbiaoExpertList.forEach(item -> {
            String pingbiaoExpertId = item.getId();
            List<ExPingbiaoExpertDetail> exPingbiaoExpertDetails = exPingbiaoExpertDetailService.selectByMainId(pingbiaoExpertId);
            for (int i = 0; i < exPingbiaoExpertDetails.size(); i++) {
                ExZbwjConformanceReviewDetaiScoreVO vo = new ExZbwjConformanceReviewDetaiScoreVO();
                ExPingbiaoExpertDetail exPingbiaoExpertDetail = exPingbiaoExpertDetails.get(i);
                BeanUtils.copyProperties(exPingbiaoExpertDetail, vo);
                vo.setTenderListId(item.getTenderListId());
                detaiScoreVOS.add(vo);
            }
        });
        return detaiScoreVOS;
    }

    private List<ExZbwjConformanceReviewDetaiScoreSumVO> getConformanceReviewSum(String conformanceReviewId, String conformanceReviewDetailId) {
        LambdaQueryWrapper<ExPingbiaoExpert> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(ExPingbiaoExpert::getRealBusinessId, conformanceReviewId);
        List<ExPingbiaoExpert> pingbiaoExpertList = exPingbiaoExpertService.list(queryWrapper1);
        List<String> tenderListIds = pingbiaoExpertList.stream().map(ExPingbiaoExpert::getTenderListId).distinct().collect(Collectors.toList());
        List<ExZbwjConformanceReviewDetaiScoreSumVO> sumVOS = new ArrayList<>();
        for (int i = 0; i < tenderListIds.size(); i++) {
            String tenderListId = tenderListIds.get(i);
            List<String> exPingbiaoExpertIdList = pingbiaoExpertList.stream().filter(e -> e.getTenderListId().equals(tenderListId)).map(ExPingbiaoExpert::getId).collect(Collectors.toList());
            LambdaQueryWrapper<ExPingbiaoExpertDetail> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ExPingbiaoExpertDetail::getBusinessId, conformanceReviewDetailId);
            queryWrapper.in(ExPingbiaoExpertDetail::getPingbiaoExpertId, exPingbiaoExpertIdList);
            queryWrapper.eq(ExPingbiaoExpertDetail::getBusinessType, "符合性审查项");
            List<ExPingbiaoExpertDetail> expertDetailList = exPingbiaoExpertDetailService.list(queryWrapper);
            int passNumber = (int) expertDetailList.stream().filter(e -> new BigDecimal(1).compareTo(e.getScore()) == 0).count();
            int failNumber = expertDetailList.size() - passNumber;
            ExZbwjConformanceReviewDetaiScoreSumVO vo = new ExZbwjConformanceReviewDetaiScoreSumVO();
            vo.setPassNumber(passNumber);
            vo.setFailNumber(failNumber);
            vo.setTenderListId(tenderListId);
            sumVOS.add(vo);
        }
        return sumVOS;
    }


    /**
     * 根据专家查询所有投标人的评分记录
     *
     * @param expertId
     * @param businessId
     * @param businessType
     * @return
     */
    @Override
    public List<ScoreChapterTree> queryPfRecordByExpertOrTender(String sectionId, String expertId, String tenderId, String businessId, String businessType) {
        LambdaQueryWrapper<ScoreChapter> query = new LambdaQueryWrapper<ScoreChapter>().eq(ScoreChapter::getScoreStandardId, businessId).eq(ScoreChapter::getStandardType, businessType);
        List<ScoreChapter> scoreChapterList = scoreChapterService.list(query);

        List<ScoreChapterTree> scoreChapterTreeList = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(scoreChapterList)) {
            List<ExZbwjScoreStandardDetail> standardDetailList = scoreStandardDetailMapper.selectByBusinessIdList(scoreChapterList.stream().map(ScoreChapter::getId).collect(Collectors.toList()));
            Map<String, ExZbwjScoreStandardDetail> standardDetailMap = Maps.newHashMap();
            for (ExZbwjScoreStandardDetail standardDetail : standardDetailList) {
                if (!standardDetailMap.containsKey(standardDetail.getCategoryId())) {
                    standardDetailMap.put(standardDetail.getCategoryId(), standardDetail);
                }
            }

            for (ScoreChapter scoreChapter : scoreChapterList) {
                ScoreChapterTree scoreChapterTree = new ScoreChapterTree();
                BeanUtils.copyProperties(scoreChapter, scoreChapterTree);
                ExZbwjScoreStandardDetail standardDetail = standardDetailMap.get(scoreChapter.getId());
                if (null != standardDetail) {
                    scoreChapterTree.setIndicatorName(standardDetail.getIndicatorName());
                    scoreChapterTree.setIndicatorType(standardDetail.getIndicatorType());
                    scoreChapterTree.setScore(standardDetail.getScore());
                    scoreChapterTree.setScoreMin(standardDetail.getScoreMin());
                    scoreChapterTree.setScoreMax(standardDetail.getScoreMax());
                    scoreChapterTree.setWeight(standardDetail.getWeight());
                    scoreChapterTree.setDescription(standardDetail.getDescription());
                }
                scoreChapterTreeList.add(scoreChapterTree);
            }
        }
        if (StringUtils.isNotBlank(expertId)) {
            List<ExPingbiaoExpert> pingbiaoExpertList = exPingbiaoExpertMapper.selectByExpertIdAndRealBusinessIdList(expertId, com.google.common.collect.Lists.newArrayList(businessId));
            if (CollectionUtils.isNotEmpty(pingbiaoExpertList)) {
                // 构建投标人map
                Map<String, String> tenderIdMap = pingbiaoExpertList.stream().collect(Collectors.toMap(ExPingbiaoExpert::getId, ExPingbiaoExpert::getTenderListId));
                List<ExTenderList> sectionTenderList = tenderListService.queryBySectionId(sectionId);
                // 查询投标人名称
                Set<String> tenderListIdList = pingbiaoExpertList.stream().map(ExPingbiaoExpert::getTenderListId).collect(Collectors.toSet());
                List<ExTenderList> tenderList = tenderListService.getBaseMapper().selectBatchIds(tenderListIdList);
                Map<String, String> tenderNameMap = tenderList.stream().collect(Collectors.toMap(ExTenderList::getId, ExTenderList::getBidderName));
                // 查询投标明细
                List<String> expertIdList = pingbiaoExpertList.stream().map(ExPingbiaoExpert::getId).collect(Collectors.toList());
                LambdaQueryWrapper<ExPingbiaoExpertDetail> detailQuery = new LambdaQueryWrapper<ExPingbiaoExpertDetail>().in(ExPingbiaoExpertDetail::getPingbiaoExpertId, expertIdList);
                List<ExPingbiaoExpertDetail> detailList = exPingbiaoExpertDetailMapper.selectList(detailQuery);
                if (CollectionUtils.isNotEmpty(detailList)) {
                    List<TenderScoreVO> tenderScoreList = detailList.stream().map(e -> {
                        TenderScoreVO tenderScoreVO = new TenderScoreVO();
                        tenderScoreVO.setId(tenderIdMap.get(e.getPingbiaoExpertId()));
                        tenderScoreVO.setName(tenderNameMap.get(tenderScoreVO.getId()));
                        tenderScoreVO.setBusinessId(e.getBusinessId());
                        tenderScoreVO.setScore(e.getScore());
                        return tenderScoreVO;
                    }).collect(Collectors.toList());
                    Map<String, List<TenderScoreVO>> businessTenderScoreMap = tenderScoreList.stream().collect(Collectors.groupingBy(TenderScoreVO::getBusinessId));
                    List<ExPingbiaoExpertDetail> expertDetailList = exPingbiaoExpertDetailMapper.selectByExpertAndBusinessId(pingbiaoExpertList.get(0).getId(), "评分标准");
                    Map<String, BigDecimal> scoreMap = expertDetailList.stream().collect(Collectors.toMap(ExPingbiaoExpertDetail::getBusinessId, ExPingbiaoExpertDetail::getScore));
                    for (ScoreChapterTree scoreChapterTree : scoreChapterTreeList) {
                        List<TenderScoreVO> scoreList = businessTenderScoreMap.getOrDefault(scoreChapterTree.getId(), Lists.newArrayList());
                        for (ExTenderList tender : sectionTenderList) {
                            long count = scoreList.stream().filter(e -> Objects.equals(tender.getId(), e.getId())).count();
                            if (count == 0) {
                                TenderScoreVO tenderScoreVO = new TenderScoreVO();
                                tenderScoreVO.setId(tender.getId());
                                tenderScoreVO.setName(tender.getBidderName());
                                tenderScoreVO.setBusinessId(scoreChapterTree.getId());
                                tenderScoreVO.setScore(BigDecimal.ZERO);
                                scoreList.add(tenderScoreVO);
                            }
                        }
                        scoreChapterTree.setExpertScore(scoreMap.get(scoreChapterTree.getId()));
                        scoreChapterTree.setScoreList(scoreList);
                    }
                }
            }
        } else if (StringUtils.isNotBlank(tenderId)) {
            List<ExPingbiaoExpert> pingbiaoTenderList = exPingbiaoExpertMapper.selectByTenderIdAndRealBusinessIdList(tenderId, com.google.common.collect.Lists.newArrayList(businessId));
            // 构建专家map
            Map<String, String> expertUserIdMap = Maps.newHashMap();
            Map<String, String> tenderNameMap = Maps.newHashMap();
            List<ExPingbiaoExpertDetail> detailList = Lists.newArrayList();
            // 查询本次评标的专家列表
            List<ExPingbiaoSectionExpertConfig> expertConfigList = sectionExpertConfigMapper.selectByProjectAndSectionId(null, sectionId);
            if (CollectionUtils.isNotEmpty(pingbiaoTenderList)) {
                expertUserIdMap = pingbiaoTenderList.stream().collect(Collectors.toMap(ExPingbiaoExpert::getId, ExPingbiaoExpert::getExpertId));

                // 查询投标明细
                List<String> expertIdList = pingbiaoTenderList.stream().map(ExPingbiaoExpert::getId).collect(Collectors.toList());
                LambdaQueryWrapper<ExPingbiaoExpertDetail> detailQuery = new LambdaQueryWrapper<ExPingbiaoExpertDetail>().in(ExPingbiaoExpertDetail::getPingbiaoExpertId, expertIdList);
                detailList = exPingbiaoExpertDetailMapper.selectList(detailQuery);
            }
            // 查询专家名称
            Set<String> expertUserIdList = expertConfigList.stream().map(ExPingbiaoSectionExpertConfig::getExpertId).collect(Collectors.toSet());
            if (CollectionUtils.isNotEmpty(expertUserIdList)) {
                List<SysUser> expertList = sysUserService.getBaseMapper().selectBatchIds(expertUserIdList);
                tenderNameMap = expertList.stream().collect(Collectors.toMap(SysUser::getId, SysUser::getUsername));
            }
            Map<String, List<TenderScoreVO>> businessTenderScoreMap = Maps.newHashMap();
            Map<String, BigDecimal> scoreMap = Maps.newHashMap();
            if (CollectionUtils.isNotEmpty(detailList)) {
                List<TenderScoreVO> tenderScoreList = Lists.newArrayList();
                for (ExPingbiaoExpertDetail e : detailList) {
                    TenderScoreVO tenderScoreVO = new TenderScoreVO();
                    tenderScoreVO.setId(expertUserIdMap.get(e.getPingbiaoExpertId()));
                    tenderScoreVO.setName(tenderNameMap.get(tenderScoreVO.getId()));
                    tenderScoreVO.setBusinessId(e.getBusinessId());
                    tenderScoreVO.setScore(e.getScore());
                    tenderScoreList.add(tenderScoreVO);
                }
                businessTenderScoreMap = tenderScoreList.stream().collect(Collectors.groupingBy(TenderScoreVO::getBusinessId));
                List<ExPingbiaoExpertDetail> expertDetailList = exPingbiaoExpertDetailMapper.selectByExpertAndBusinessId(pingbiaoTenderList.get(0).getId(), "评分标准");
                scoreMap = expertDetailList.stream().collect(Collectors.toMap(ExPingbiaoExpertDetail::getBusinessId, ExPingbiaoExpertDetail::getScore));
            }
            for (ScoreChapterTree scoreChapterTree : scoreChapterTreeList) {
                List<TenderScoreVO> tenderScoreList = businessTenderScoreMap.getOrDefault(scoreChapterTree.getId(), Lists.newArrayList());
                for (ExPingbiaoSectionExpertConfig expert : expertConfigList) {
                    long count = tenderScoreList.stream().filter(e -> Objects.equals(expert.getExpertId(), e.getId())).count();
                    if (count == 0) {
                        TenderScoreVO tenderScoreVO = new TenderScoreVO();
                        tenderScoreVO.setId(expert.getExpertId());
                        tenderScoreVO.setScore(BigDecimal.ZERO);
                        tenderScoreVO.setName(tenderNameMap.get(expert.getExpertId()));
                        tenderScoreVO.setBusinessId(scoreChapterTree.getId());
                        tenderScoreList.add(tenderScoreVO);
                    }
                }
                scoreChapterTree.setExpertScore(scoreMap.get(scoreChapterTree.getId()));
                scoreChapterTree.setScoreList(tenderScoreList);
            }

        }
        List<ScoreChapterTree> treeList = Lists.newArrayList();
        getTreeList(treeList, scoreChapterTreeList, null);
        return treeList;
    }

    @Override
    public ExPingbiaoConformanceReview getConformanceReviewExpertRecord(String conformanceReviewId, String tenderListId) {
        ExPingbiaoConformanceReview conformanceReview = new ExPingbiaoConformanceReview();
        List<ExZbwjConformanceReviewDetailVO> conformanceReviewDetailVOList = new ArrayList<>();
        ExZbwjConformanceReview review = exZbwjConformanceReviewService.getById(conformanceReviewId);
        String bidSectionId = review.getBidSectionIds();

        List<ExZbwjConformanceReviewDetail> exZbwjConformanceReviewDetails = exZbwjConformanceReviewDetailService.selectByMainId(conformanceReviewId);
        for (int i = 0; i < exZbwjConformanceReviewDetails.size(); i++) {
            ExZbwjConformanceReviewDetailVO vo = new ExZbwjConformanceReviewDetailVO();
            ExZbwjConformanceReviewDetail exZbwjConformanceReviewDetail = exZbwjConformanceReviewDetails.get(i);
            BeanUtils.copyProperties(exZbwjConformanceReviewDetail, vo);
            conformanceReviewDetailVOList.add(vo);
            //赋值 审查项下 指定投标人  所有专家的是审查项 评分记录
            vo.setScoreList(getAllExpertScore(exZbwjConformanceReviewDetail.getConformanceReviewId(), exZbwjConformanceReviewDetail.getId(), tenderListId));
        }
        conformanceReview.setConformanceReviewDetailList(conformanceReviewDetailVOList);

        //投标人
        setExTenderList(conformanceReview, bidSectionId);
        return conformanceReview;
    }

    private List<ExZbwjConformanceReviewDetaiScoreVO> getAllExpertScore(String conformanceReviewId, String conformanceReviewDetailId, String tenderListId) {
        LambdaQueryWrapper<ExPingbiaoExpert> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(ExPingbiaoExpert::getRealBusinessId, conformanceReviewId);
        queryWrapper1.eq(ExPingbiaoExpert::getTenderListId, tenderListId);
        List<ExPingbiaoExpert> pingbiaoExpertList = exPingbiaoExpertService.list(queryWrapper1);

        List<String> expertIdList = pingbiaoExpertList.stream().map(ExPingbiaoExpert::getExpertId).distinct().collect(Collectors.toList());
        List<ExZbwjConformanceReviewDetaiScoreVO> detaiScoreVOS = new ArrayList<>();
        for (int i = 0; i < expertIdList.size(); i++) {
            String expertId = expertIdList.get(i);
            List<String> exPingbiaoExpertIdList = pingbiaoExpertList.stream().filter(e -> e.getExpertId().equals(expertId)).map(ExPingbiaoExpert::getId).collect(Collectors.toList());
            LambdaQueryWrapper<ExPingbiaoExpertDetail> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ExPingbiaoExpertDetail::getBusinessId, conformanceReviewDetailId);
            queryWrapper.in(ExPingbiaoExpertDetail::getPingbiaoExpertId, exPingbiaoExpertIdList);
            queryWrapper.eq(ExPingbiaoExpertDetail::getBusinessType, "符合性审查项");
            List<ExPingbiaoExpertDetail> expertDetailList = exPingbiaoExpertDetailService.list(queryWrapper);
            expertDetailList.forEach((item -> {
                ExZbwjConformanceReviewDetaiScoreVO vo = new ExZbwjConformanceReviewDetaiScoreVO();
                BeanUtils.copyProperties(item, vo);
                vo.setExpertId(expertId);
                detaiScoreVOS.add(vo);
            }));
        }
        return detaiScoreVOS;
    }

    private void getTreeList(List<ScoreChapterTree> treeList, List<ScoreChapterTree> metaList, ScoreChapterTree temp) {
        for (ScoreChapterTree scoreChapterTree : metaList) {
            String tempPid = scoreChapterTree.getPid();
            ScoreChapterTree tree = new ScoreChapterTree();
            BeanUtils.copyProperties(scoreChapterTree, tree);
            ;
            if (temp == null && (oConvertUtils.isEmpty(tempPid) || "0".equals(tempPid))) {
                treeList.add(tree);
                if ("1".equals(tree.getHasChild())) {
                    getTreeList(treeList, metaList, tree);
                }
            } else if (temp != null && tempPid != null && tempPid.equals(temp.getId())) {
                if (null == temp.getChildren()) {
                    temp.setChildren(Lists.newArrayList());
                }
                temp.getChildren().add(tree);
                if ("1".equals(tree.getHasChild())) {
                    getTreeList(treeList, metaList, tree);
                }
            }
        }
    }


    /**
     * 查询投标人评分分数汇总
     *
     * @param businessId
     * @param businessType
     * @return
     */
    @Override
    public List<ScoreChapterTree> queryTenderPfSum(String sectionId, String businessId, String businessType) {
        LambdaQueryWrapper<ScoreChapter> query = new LambdaQueryWrapper<ScoreChapter>().eq(ScoreChapter::getScoreStandardId, businessId).eq(ScoreChapter::getStandardType, businessType);
        List<ScoreChapter> scoreChapterList = scoreChapterService.list(query);

        List<ScoreChapterTree> scoreChapterTreeList = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(scoreChapterList)) {
            List<ExZbwjScoreStandardDetail> standardDetailList = scoreStandardDetailMapper.selectByBusinessIdList(scoreChapterList.stream().map(ScoreChapter::getId).collect(Collectors.toList()));
            Map<String, ExZbwjScoreStandardDetail> standardDetailMap = Maps.newHashMap();
            for (ExZbwjScoreStandardDetail standardDetail : standardDetailList) {
                if (!standardDetailMap.containsKey(standardDetail.getCategoryId())) {
                    standardDetailMap.put(standardDetail.getCategoryId(), standardDetail);
                }
            }
            for (ScoreChapter scoreChapter : scoreChapterList) {
                ScoreChapterTree scoreChapterTree = new ScoreChapterTree();
                BeanUtils.copyProperties(scoreChapter, scoreChapterTree);
                ExZbwjScoreStandardDetail standardDetail = standardDetailMap.get(scoreChapter.getId());
                if (null != standardDetail) {
                    scoreChapterTree.setIndicatorName(standardDetail.getIndicatorName());
                    scoreChapterTree.setIndicatorType(standardDetail.getIndicatorType());
                    scoreChapterTree.setScore(standardDetail.getScore());
                    scoreChapterTree.setScoreMin(standardDetail.getScoreMin());
                    scoreChapterTree.setScoreMax(standardDetail.getScoreMax());
                    scoreChapterTree.setWeight(standardDetail.getWeight());
                    scoreChapterTree.setDescription(standardDetail.getDescription());
                }
                scoreChapterTreeList.add(scoreChapterTree);
            }
        }
        List<ExPingbiaoExpert> sumPingbiaoExpertList = exPingbiaoExpertMapper.sumByRealBusinessIdList(businessId);
        // 构建投标人map
        List<ExTenderList> sectionTenderList = tenderListService.queryBySectionId(sectionId);
        Map<String, ExPingbiaoExpert> tenderScoreMap = sumPingbiaoExpertList.stream().collect(Collectors.toMap(e -> e.getTenderListId() + "_" + e.getBusinessId(), Function.identity()));
        for (ScoreChapterTree scoreChapterTree : scoreChapterTreeList) {
            List<TenderScoreVO> scoreList = Lists.newArrayList();
            for (ExTenderList tender : sectionTenderList) {
                String key = tender.getId() + "_" + scoreChapterTree.getId();
                ExPingbiaoExpert list = tenderScoreMap.get(key);
                if (list == null) {
                    TenderScoreVO tenderScoreVO = new TenderScoreVO();
                    tenderScoreVO.setId(tender.getId());
                    tenderScoreVO.setName(tender.getBidderName());
                    tenderScoreVO.setBusinessId(scoreChapterTree.getId());
                    tenderScoreVO.setScore(BigDecimal.ZERO);
                    scoreList.add(tenderScoreVO);
                } else {
                    TenderScoreVO tenderScoreVO = new TenderScoreVO();
                    tenderScoreVO.setId(tender.getId());
                    tenderScoreVO.setName(tender.getBidderName());
                    tenderScoreVO.setBusinessId(scoreChapterTree.getId());
                    tenderScoreVO.setScore(list.getScore());
                    scoreList.add(tenderScoreVO);
                }
            }
            scoreChapterTree.setScoreList(scoreList);
        }
        List<ScoreChapterTree> treeList = Lists.newArrayList();
        getTreeList(treeList, scoreChapterTreeList, null);
        return treeList;
    }

}
